<?php

namespace AuctionAppApi\Controller;

use Think\Controller;
use AuctionCore\System\AuctionException;
use AuctionCore\Common\Util;
use AuctionCore\Common\PhpRedis;
use AuctionCore\Account\UniqLogin;
use AuctionCore\Account\LoginLog;


/**
 * AppApi抽象控制器
 */
class BaseController extends Controller {
    public $dealer_id = 0;
    public $order_id = 0;

    public $access_key = false;
    public $uuid = false;
    public $uid = false;
    public $time = false;
    public $rand_num = false;
    public $is_ios = false;

    //公开访问的action
    protected  $exclude_c = ARRAY(
        'C_DEMO|A_TEST'
        ,'C_AUTH|A_LOGIN'
        ,'C_AUTH|A_LOGOUT'
        ,'C_SYSTEM|A_GET_SYSTEM_TIME'
        ,'C_CAROUSEL|A_INDEX'
        ,'C_SYSTEM|A_GET_BASIC_CONFIG'
        ,'C_RECHARGE|A_JUMP'
        ,'C_RECHARGE|A_RESULT'
        ,'C_RECHARGE|A_BACKGROUND'
        ,'C_RECHARGE|A_RECHARGE_AGAIN'
        ,'C_RECHARGE|A_CHECK_ACCOUNTS'
        ,'C_RECHARGE|A_PUBLIC_CHECK_ALIPAY'
        ,'C_RAFFLE|A_GET_FACE_VALUE'
        ,'C_RAFFLE|A_GET_DEALER_PHONE_CARD'
        ,'C_RAFFLE|A_RAFFLEACTIVITY'
        ,'C_RAFFLE|A_COMMON_ACTIVITY'
        ,'C_SYSTEM|A_GET_CHIP'
        ,'C_SHARE|A_REPORT'
        ,'C_TRADE|A_PDF'
        ,'C_TRADE|A_PACT'
        ,'C_SYSTEM|A_GETKEY'
        ,'C_AUTH|A_LOGIN_V2'
        ,'C_AUTH|A_GET_MANAGER'
        ,'C_SYSTEM|A_GETSMS'
        ,'C_SYSTEM|A_GETCITYS'
        ,'C_RECHARGE|A_NOTIFYALIPAY'
        ,'C_DEALER|A_CHANGE_PWD_FORCE'
        ,'C_DEALER|A_SEND_AUTH_CODE'
        ,'C_AUTH|A_REGIST'
        ,'C_AUTH|A_RESET_PWD'
        ,'C_CONSUMER|A_MUSTREAD'
    //  ,'C_DEALER|A_INDEX_V1'
        ,'C_SHARE|A_REPAIR_SHARE'
        ,'C_AUTH|A_AUTH_SIGN'

        //维保
        ,'C_REPAIR|A_NOTIFYCJD'
        ,'C_REPAIR|A_NOTIFYCBS'
        //帮砍
        ,'C_DEALER|A_INVITE'
        ,'C_DEALER|A_CUTDOWN'
        ,'C_DEALER|A_SEND_VERIFY_CODE'
        ,'C_DEALER|A_DO_CUTDOWN'
    );
    protected  $h5_actions = array();//appH5
    protected  $html5_actions = array();
    //初始化
    public function __construct() {

        $ca = strtoupper('C_'.CONTROLLER_NAME."|A_".ACTION_NAME);
        if(in_array( $ca,$this->h5_actions)){
            //h5异常处理输出
            set_exception_handler('AuctionCore\System\AuctionException::appH5Exception');
        }elseif(in_array( $ca,$this->html5_actions)){
            //api异常处理输出
            set_exception_handler('AuctionCore\System\AuctionException::H5Exception');
        }else{
            //api异常处理输出
            set_exception_handler('AuctionCore\System\AuctionException::appException');
        }

        parent::__construct();
        $this->dealer_id = I('request.dealer_id');
        $this->order_id = I('request.order_id'); 

        $this->access_key = I('request.access_key');       
        $this->uuid = I('request.uuid');       
        $this->platform = I('request.platform');
        $this->uid = I('request.uid');
        $this->time = I('request.time');
        $this->rand_num = substr($this->time,-3);
        $this->version = I('request.version');

        //如果IOS_APP在提交苹果商店审核中,则使用测试库
        $this->is_ios = $this->platform=='ios' ? true :false;
        if(C('IOS_APP_AUDITING') && $this->is_ios && C('IOS_APP_AUDITING_VER')==$this->version){
            C(array('DB_NAME'=>C('DB_NAME_TEST')));
        }

        //页面头部信息
        $head = C('HEAD_INDEX');
        $this->assign("web_title", $head['title']);
        $this->assign("web_keywords", $head['keywords']);
        $this->assign("web_description", $head['description']);


        $ca = strtoupper('C_'.CONTROLLER_NAME."|A_".ACTION_NAME);
        if(!in_array( $ca,$this->exclude_c)){
       // 取消验证 统一使用蚁巢服务验证状态     $this->check_user_limit($this->uid);    //检查帐号是否被禁用
            $this->check_uniq_login($this->uuid,$this->uid);    //检查唯一登录
            //$this->check_access_key();  //检查access_key
        }
    }
    
    //api正确结果输出
    public function api_200($data){
        echo Util::auto_jsonp_or_json($_REQUEST,array('code'=>200,'msg'=>'success','data'=>$data));
        exit;
    }

    //检查唯一登录
    private function check_uniq_login($uuid,$uid){  
        
        $redis = new PhpRedis();
        $ul = new UniqLogin();
        $arr = [
            'uuid'=>$uuid,
            'uid'=>$uid
        ];
        $keyName = getRedisKeyByCode('REDIS_KEY','100003',$arr);
        $token = $redis->get($keyName);
        if(!$token)
            AuctionException::make_throw('200037');  
        $ok = $ul->check($uid,$token);
        if(!$ok)
            AuctionException::make_throw('200035'); 
             
        //更新登录活动记录
        LoginLog::up_act_time(sha1($token));
    }

}
